import { defineStore } from 'pinia'
import { AppConst } from '@/constants/system/app-const'
import { LocalStorageKeyConst } from '@/constants/framework/local-storage-key-const'
import { errorSentry } from '@/lib/error-sentry'
import { AppConfig } from '@/types/app-config-types'
import { useStorage } from '@/hooks/web/use-storage-hook'
let state = {
    ...AppConst
}
const { getStorage } = useStorage()
let appConfigStr = getStorage(LocalStorageKeyConst.APP_CONFIG)

let language = AppConst.language
if (appConfigStr) {
    try {
        state = appConfigStr
        language = state.language
    } catch (e) {
        errorSentry.captureError(e)
    }
}

/**
 * 获取初始化的语言
 */
export const getInitializedLanguage = function () {
    return language
}
/*
 * 应用的配置信息
 * @export useAppConfigStore
 */
interface AppConfigStore extends AppConfig {
    layoutContentClientRect: Omit<DOMRect, 'toJSON'>
}
export const useAppConfigStore = defineStore('appConfig', {
    state: (): AppConfigStore => ({
        // 读取config下的默认配置
        ...state,
        layoutContentClientRect: {
            x: 0,
            y: 0,
            width: 0,
            height: 0,
            bottom: 0,
            left: 0,
            right: 0,
            top: 0
        },
    }),
    getters: {
        getLayoutContentClientRect(): Omit<DOMRect, 'toJSON'> {
            return this.layoutContentClientRect
        },
    },
    actions: {
        reset() {
            for (const k in AppConst) {
                this[k] = AppConst[k]
            }
        },
        showHelpDoc() {
            this.helpDocExpandFlag = true
        },
        hideHelpDoc() {
            this.helpDocExpandFlag = false
        },
        startFullScreen() {
            this.fullScreenFlag = true
        },
        exitFullScreen() {
            this.fullScreenFlag = false
        },
        setSideMenuWidth(width: number) {
            this.sideMenuWidth = width
        },
        setLayoutContentClientRect(DOMRect: DOMRect) {
            this.layoutContentClientRect = DOMRect.toJSON()
        },
        // 计算内容区域中的表格高度
        calculateTableHeight(height: number) {
            const tableHeight = this.layoutContentClientRect.height - height
            console.log(this.layoutContentClientRect.height , height);
        }
    }
})
